﻿/*
 * Created by SharpDevelop.
 * User: Administrator
 * Date: 2016/11/8
 * Time: 15:44
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using NetCDFDotnet;

namespace GeoFly
{
	/// <summary>
	/// Description of NC_Data.
	/// </summary>
	public class NC_Data
	{
		public int[] start = null;
		public int[] length = null;
		public int rowCount = 0;
		public int colCount = 0;
		public NCVariable nc_var = null;
		public NC_Type dataType;
		private double[,] data = null;
		public bool needFlip = false;
		public double[,] Data {
			get {
				if (this.data != null)
					return data;
				double[,] result = new double[rowCount, colCount];
				double factor = 1;
				double offset = 0;
				if (nc_var.Atributes.ContainsKey("scale_factor")) {
					factor = Convert.ToDouble(nc_var.Atributes["scale_factor"].value);
					offset = Convert.ToDouble(nc_var.Atributes["add_offset"].value);
				}
				if (nc_var.nc_type == NetCDFDotnet.NC_Type.NC_FLOAT) {
					float[] values = nc_var.ReadValuesFloat(start, length);
					for (int row = 0; row < rowCount; row++) {
						for (int col = 0; col < colCount; col++) {
							double v = 0;
							if (needFlip) {
								v = (double)values[(rowCount - row - 1) * colCount + col];
							} else {
								v = (double)values[row * colCount + col];
							}	
							v = v * factor + offset;
							result[row, col] = v;
						}						
					}
					data = result;
					return result;
				}
				if (nc_var.nc_type == NetCDFDotnet.NC_Type.NC_INT) {
					int[] values = nc_var.ReadValuesInt(start, length);
					for (int row = 0; row < rowCount; row++) {
						for (int col = 0; col < colCount; col++) {
							double v = 0;
							if (needFlip) {
								v = (double)values[(rowCount - row - 1) * colCount + col];
							} else {
								v = (double)values[row * colCount + col];
							}	
							v = v * factor + offset;
							result[row, col] = v;
						}
					}				
					data = result;
					return result;
				}
				if (nc_var.nc_type == NetCDFDotnet.NC_Type.NC_SHORT) {
					short[] values = nc_var.ReadValuesShort(start, length);
					for (int row = 0; row < rowCount; row++) {
						for (int col = 0; col < colCount; col++) {
							double v = 0;
							if (needFlip) {
								v = (double)values[(rowCount - row - 1) * colCount + col];
							} else {
								v = (double)values[row * colCount + col];
							}	
							v = v * factor + offset;
							result[row, col] = v;
							
						}						
					}
					data = result;
					return result;
				}
				if (nc_var.nc_type == NetCDFDotnet.NC_Type.NC_BYTE) {
					byte[] values = nc_var.ReadValuesByte(start, length);
					for (int row = 0; row < rowCount; row++) {
						for (int col = 0; col < colCount; col++) {
							double v = 0;
							if (needFlip) {
								v = (double)values[(rowCount - row - 1) * colCount + col];
							} else {
								v = (double)values[row * colCount + col];
							}	
							v = v * factor + offset;
							result[row, col] = v;
						}						
					}
					data = result;
					return result;
				}
				if (nc_var.nc_type == NetCDFDotnet.NC_Type.NC_DOUBLE) {
					double[] values = nc_var.ReadValuesDouble(start, length);
					for (int row = 0; row < rowCount; row++) {
						for (int col = 0; col < colCount; col++) {
							double v = 0;
							if (needFlip) {
								v = (double)values[(rowCount - row - 1) * colCount + col];
							} else {
								v = (double)values[row * colCount + col];
							}	
							v = v * factor + offset;
							result[row, col] = v;
						}						
					}
					data = result;
					return result;
				}
				throw new Exception("Cannot recognize the type:" + nc_var.nc_type);
			}
		}
		public NC_Data()
		{
		}
	}
}
